package org.example.erat.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.erat.dto.ClassDTO;
import org.example.erat.dto.ResponseDTO;
import org.example.erat.service.ClassService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 班级控制器
 */
@RestController
@RequestMapping("/classes")
@RequiredArgsConstructor
@Slf4j
public class ClassController {

    private final ClassService classService;

    /**
     * 创建班级
     * @param classDTO 班级DTO
     * @return 响应
     */
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseDTO<ClassDTO> createClass(@Valid @RequestBody ClassDTO classDTO) {
        log.info("创建班级请求: {}", classDTO);
        ClassDTO createdClass = classService.createClass(classDTO);
        return ResponseDTO.success("班级创建成功", createdClass);
    }

    /**
     * 更新班级
     * @param id 班级ID
     * @param classDTO 班级DTO
     * @return 响应
     */
    @PutMapping("/{id}")
    public ResponseDTO<ClassDTO> updateClass(@PathVariable Long id, @Valid @RequestBody ClassDTO classDTO) {
        log.info("更新班级请求: id={}, class={}", id, classDTO);
        ClassDTO updatedClass = classService.updateClass(id, classDTO);
        return ResponseDTO.success("班级更新成功", updatedClass);
    }

    /**
     * 删除班级
     * @param id 班级ID
     * @return 响应
     */
    @DeleteMapping("/{id}")
    public ResponseDTO<Void> deleteClass(@PathVariable Long id) {
        log.info("删除班级请求: id={}", id);
        classService.deleteClass(id);
        return ResponseDTO.success("班级删除成功", null);
    }

    /**
     * 获取班级
     * @param id 班级ID
     * @return 响应
     */
    @GetMapping("/{id}")
    public ResponseDTO<ClassDTO> getClass(@PathVariable Long id) {
        log.info("获取班级请求: id={}", id);
        ClassDTO classDTO = classService.getClass(id);
        return ResponseDTO.success(classDTO);
    }

    /**
     * 获取所有班级
     * @return 响应
     */
    @GetMapping
    public ResponseDTO<List<ClassDTO>> getAllClasses() {
        log.info("获取所有班级请求");
        List<ClassDTO> classes = classService.getAllClasses();
        return ResponseDTO.success(classes);
    }

    /**
     * 根据课程ID获取班级列表
     * @param courseId 课程ID
     * @return 响应
     */
    @GetMapping("/course/{courseId}")
    public ResponseDTO<List<ClassDTO>> getClassesByCourse(@PathVariable Long courseId) {
        log.info("获取课程的班级列表请求: courseId={}", courseId);
        List<ClassDTO> classes = classService.getClassesByCourse(courseId);
        return ResponseDTO.success(classes);
    }
}
